package com.jianzong.lingxuan.api.controller;

import cn.hutool.core.util.ObjectUtil;
import com.anji.captcha.model.vo.CaptchaVO;
import com.jianzong.lingxuan.api.service.ApiService;
import com.jianzong.lingxuan.base.Result;
import com.jianzong.lingxuan.error.SysError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @auth Leiwenlong
 * @since 2021/7/19
 */
@RestController
@RequestMapping("/api")
public class ApiController {

    public final ApiService apiService;

    public ApiController(ApiService apiService){
        this.apiService = apiService;
    }

    @GetMapping("/base")
    @CrossOrigin
    public Result action(HttpServletRequest request, HttpServletResponse response){
        //1.获取所有请求参数
        Map<String, String[]> parameterMap = request.getParameterMap();

        CaptchaVO captchaVO = new CaptchaVO();

        String token = "";
        if(ObjectUtil.isNotNull(request.getHeader("token"))){
            token = request.getHeader("token");
        }

        response.setHeader("Access-Control-Allow-Origin", "*");

        if(0 == parameterMap.size()){
            return Result.error();
        }else{
            //调用
            return apiService.action(parameterMap,token,captchaVO);
        }

    }
}
